package com.feeyo.groundservice.base_util.loading

import android.annotation.SuppressLint
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.feeyo.groundservice.base_util.R
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.impl.LoadingPopupView

/**
 *作者：QLH on 2022-10-17
 *描述：第三方loading
 */
object LoadingPopViewUtil : ILoading {

    @SuppressLint("StaticFieldLeak")
    @Volatile
    private var loading: LoadingPopupView? = null

    override fun show(msg: String?, dismissOnBackPressed: Boolean) {
        if (loading?.isShown == true) loading?.dismiss()//清除前一个loading
        if (!AppUtils.isAppForeground()) return

        loading = XPopup.Builder(ActivityUtils.getTopActivity())
            .dismissOnTouchOutside(false)
            .isLightNavigationBar(true)
            .dismissOnBackPressed(dismissOnBackPressed)
            .hasShadowBg(false)
            .asLoading(
                msg ?: "正在加载...",
                R.layout.view_pop_loading,
                LoadingPopupView.Style.Spinner
            )
        loading?.show()
    }

    override fun close() {
        loading?.apply {
            if (isShown) {
                dismiss()
            }
        }
        loading = null
    }

    override fun smartClose() {
        loading?.apply {
            if (isShown) {
                smartDismiss()
            }
        }
        loading = null
    }
}